using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyTaskHit : Node
{
    private EnemyControl enemyControl;
    /********变量*********/
    private float stateTime = 0f;
    
    // 
    public EnemyTaskHit(EnemyControl enemyControl)
    {
        this.enemyControl = enemyControl;
    }

    //
    public override NodeState Evaluate()
    {
        // 受到伤害执行成功
        if (enemyControl.IsHit)
        {
            // 速度设置为零
            enemyControl.GetRb.velocity = Vector2.zero;
            // 受到伤害首先碰撞体取消激活
            enemyControl.GetMineBox2D.enabled = false;
            // 播放动画
            enemyControl.GetAnimator.Play("SlimeGHit");
            // 动画播放时长计时
            stateTime += Time.deltaTime;
            // 如果动画播放完成
            if (stateTime > enemyControl.GetAnimator.GetCurrentAnimatorStateInfo(0).length)
            {
                // 取消激活脚
                enemyControl.GetFoot.SetActive(false);
                // 结束受伤
                enemyControl.IsHit = false;
                // 计时清零
                stateTime = 0;
                // 返回Idle
                enemyControl.GetAnimator.Play("SlimeGRun");
                // 取消激活小怪
                enemyControl.gameObject.SetActive(false);
                // 继续执行，进入死亡节点
                state = NodeState.SUCCESS;
                return state;
            }
            // 默认状态为RUNNING，持续执行
            state = NodeState.RUNNING;
            return state;
        }
        
        // 默认状态为失败，返回失败
        state = NodeState.FAILURE;
        return state;
    }
}
